//
//  YTHDetailHeaderView.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/7/16.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTHDetailHeaderView.h"
@interface YTHDetailHeaderView ()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *pTimeLabel;
@property (weak, nonatomic) IBOutlet UIImageView *avatorImgView;
@property (weak, nonatomic) IBOutlet UILabel *authorLabel;
@property (weak, nonatomic) IBOutlet UILabel *tipLabel;
@property (weak, nonatomic) IBOutlet UIButton *followButton;

@end
@implementation YTHDetailHeaderView

// 实例化
+ (instancetype)headerViewWithFrame:(CGRect)frame
{
    UINib *nib = [UINib nibWithNibName:@"YTHDetailHeaderView" bundle:nil];
    YTHDetailHeaderView *v = [nib instantiateWithOwner:nil options:nil][0];
    v.frame = frame;
    return v;
}

- (IBAction)followButtonClick:(UIButton *)sender
{
    if (!YTNetworkManager.shareManager.account.auth) {
        [NSNotificationCenter.defaultCenter postNotificationName:kShouldLoginNotication
                                                          object:nil];
        return;
    }
    
    [YTNetworkManager.shareManager requestFollowAuthorWithFollowID:self.model.uid isFollow:sender.selected
                                                        completion:^(BOOL isSucc, BOOL isReward,NSString *error)
    {
        if (!isSucc) {
            [self.superview showTipWithTipStr:error];
            return ;
        }
        
        // 关注成功
        sender.selected = !sender.selected;
        
        isReward ? [self.superview showAwardWithTipStr:[NSString stringWithFormat:@"关注成功+%@算力",error]] : [self.superview showTipWithTipStr:error];
        
    }];
    
}

- (void)setModel:(YTHomeDetailModel *)model
{
    _model = model;
    
    // 设置数据
    self.titleLabel.text = model.post_title;
    self.pTimeLabel.text = model.post_date;
    [self.avatorImgView sd_setImageWithURL:[NSURL URLWithString:model.user_avatar]];
    self.authorLabel.text = model.display_name;
    self.tipLabel.text = model.decs;
    //NSLog(@"%@",model.isRecommend);
    self.followButton.selected = model.isCareAuthor.boolValue;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    [self setupUI];
    
    
    self.avatorImgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iconClick)];
    [self.avatorImgView addGestureRecognizer:tap];
   
}

- (void)iconClick
{
    NSAssert(self.authorIconClickBlock != nil, @"authorIconClickBlock 不为nil");
    self.authorIconClickBlock(self.model.uid);
}

- (void)setupUI
{
    self.followButton.layer.masksToBounds = YES;
    self.followButton.layer.cornerRadius = 5;
    self.followButton.layer.borderWidth = 1;
    self.followButton.layer.borderColor = [[UIColor colorWithRed:24.0f/255.0f
                                                           green:170.0f/255.0f
                                                            blue:178.0f/255.0f
                                                           alpha:1] CGColor];
    
    
}
@end
